import { computed, toValue } from 'vue'
import {
  COMPANY_PRODUCT_LIST,
  CONDITION_TABLE_OF_MAJOR_ENERGY_CONSUMING_EQUIPMENT,
  DETAILED_LIST_OF_COMPANY_INDICATORS,
  ENERGY_CONSUMPTION_STRUCTURE_SUB_TABLE,
  ENERGY_CONSUMPTION_STRUCTURE_TABLE,
  ENERGY_SAVING_RENOVATION_PROJECT_INFORMATION_TABLE,
  ENTERPRISE_BASIC_INFORMATION_TABLE_ID,
  PROGRESS_ENERGY_SAVING_TARGET_COMPLETION_TABLE,
  TABLE_ACCOUNT_CONFIGS,
  TABLE_OF_COMPREHENSIVE_ENERGY_CONSUMPTION_PER_UNIT_PRODUCT,
} from './config'
import EnterpriseBasicInformationTable from './EnterpriseBasicInformationTable.vue'
import EnergyConsumptionStructureTable from './EnergyConsumptionStructureTable.vue'
import EnergyConsumptionStructureSubTable from './EnergyConsumptionStructureSubTable.vue'
import TableOfComprehensiveEnergyConsumptionPerUnitProduct from './TableOfComprehensiveEnergyConsumptionPerUnitProduct.vue'
import ProgressEnergySavingTargetCompletionTable from './ProgressEnergySavingTargetCompletionTable.vue'
import EnergySavingRenovationProjectInformationTable from './EnergySavingRenovationProjectInformationTable.vue'
import ConditionTableOfMajorEnergyConsumingEquipment from './ConditionTableOfMajorEnergyConsumingEquipment.vue'
import DetailedListOfCompanyIndicators from './DetailedListOfCompanyIndicators.vue'
import CompanyProductList from './CompanyProductList.vue'
import type { ReportColumnsItem } from './types'
import { useStatementStore } from '@/store'

export type StatementComponent =
  | typeof EnterpriseBasicInformationTable
  | typeof CompanyProductList
  | typeof TableOfComprehensiveEnergyConsumptionPerUnitProduct
  | typeof DetailedListOfCompanyIndicators
  | typeof ProgressEnergySavingTargetCompletionTable
  | typeof EnergyConsumptionStructureTable
  | typeof EnergyConsumptionStructureSubTable
  | typeof EnergySavingRenovationProjectInformationTable
  | typeof ConditionTableOfMajorEnergyConsumingEquipment
  | null

export interface TableConfig {
  ID: number
  NAME: string
  COLUMNS: ReportColumnsItem[]
}

export default function useReportComponent() {
  const statementStore = useStatementStore()
  const statementId = computed(() => statementStore.statementId)
  const tId = statementId
  const isEnterpriseBasicInformationTable = computed(() => {
    return toValue(tId) === ENTERPRISE_BASIC_INFORMATION_TABLE_ID
  })
  const isProgressEnergySavingTargetCompletionTable = computed(() => {
    return toValue(tId) === PROGRESS_ENERGY_SAVING_TARGET_COMPLETION_TABLE.ID
  })
  const isTableOfComprehensiveEnergyConsumptionPerUnitProduct = computed(() => {
    return (
      toValue(tId)
      === TABLE_OF_COMPREHENSIVE_ENERGY_CONSUMPTION_PER_UNIT_PRODUCT.ID
    )
  })
  const isConditionTableOfMajorEnergyConsumingEquipment = computed(() => {
    return (
      toValue(tId) === CONDITION_TABLE_OF_MAJOR_ENERGY_CONSUMING_EQUIPMENT.ID
    )
  })
  const isEnergySavingRenovationProjectInformationTable = computed(() => {
    return (
      toValue(tId) === ENERGY_SAVING_RENOVATION_PROJECT_INFORMATION_TABLE.ID
    )
  })
  const isEnergyConsumptionStructureTable = computed(() => {
    return toValue(tId) === ENERGY_CONSUMPTION_STRUCTURE_TABLE.ID
  })
  const isEnergyConsumptionStructureSubTable = computed(() => {
    return toValue(tId) === ENERGY_CONSUMPTION_STRUCTURE_SUB_TABLE.ID
  })
  const isCompanyProductList = computed(() => {
    return toValue(tId) === COMPANY_PRODUCT_LIST.ID
  })
  const isDetailedListOfCompanyIndicators = computed(() => {
    return toValue(tId) === DETAILED_LIST_OF_COMPANY_INDICATORS.ID
  })

  const statementComponent = computed(() => {
    let component: StatementComponent = null
    if (toValue(isEnterpriseBasicInformationTable)) {
      component = EnterpriseBasicInformationTable
    }
    else if (toValue(isCompanyProductList)) {
      component = CompanyProductList
    }
    else if (toValue(isTableOfComprehensiveEnergyConsumptionPerUnitProduct)) {
      component = TableOfComprehensiveEnergyConsumptionPerUnitProduct
    }
    else if (toValue(isDetailedListOfCompanyIndicators)) {
      component = DetailedListOfCompanyIndicators
    }
    else if (toValue(isProgressEnergySavingTargetCompletionTable)) {
      component = ProgressEnergySavingTargetCompletionTable
    }
    else if (toValue(isEnergyConsumptionStructureTable)) {
      component = EnergyConsumptionStructureTable
    }
    else if (toValue(isEnergyConsumptionStructureSubTable)) {
      component = EnergyConsumptionStructureSubTable
    }
    else if (toValue(isEnergySavingRenovationProjectInformationTable)) {
      component = EnergySavingRenovationProjectInformationTable
    }
    else if (toValue(isConditionTableOfMajorEnergyConsumingEquipment)) {
      component = ConditionTableOfMajorEnergyConsumingEquipment
    }
    return component
  })

  /**
   * 根据报表ID查询报表配置信息
   * @param id 报表ID
   * @returns 报表对象
   */
  function findTable(id: number | null): TableConfig | null {
    if (!id)
      return null
    const tables = Object.keys(TABLE_ACCOUNT_CONFIGS)
    const key = tables.find(
      key => (TABLE_ACCOUNT_CONFIGS as any)[key].ID === Number(id),
    )
    return key ? (TABLE_ACCOUNT_CONFIGS as any)[key] : null
  }

  /**
   * 根据报表ID查询报表columns配置
   * @param id 报表ID
   * @returns 报表数据列配置
   */
  function findColumns(id: number) {
    const table = findTable(id)
    return table ? table.COLUMNS : []
  }

  return {
    statementComponent,
    findTable,
    findColumns,
  }
}
